package com.hkts.naturalgasauthentication.base.serviceimpl;

import cn.hutool.core.util.StrUtil;
import com.hkts.naturalgasauthentication.base.dao.*;
import com.hkts.naturalgasauthentication.base.entity.*;
import com.hkts.naturalgasauthentication.base.service.HontUserService;
import com.hkts.naturalgasauthentication.base.vo.UserRolePermissionVo;
import com.hkts.naturalgasauthentication.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


@Service
@Transactional
public class HontUserServiceimpl implements HontUserService {
    @Autowired
    private HontUserDao hontUserDao;
    @Autowired
    private HontRoleDao hontRoleDao;
    @Autowired
    private HontUserRoleDao hontUserRoleDao;
    @Autowired
    private SecurityUtil securityUtil;
    @Autowired
    private HontRolePermissionDao hontRolePermissionDao;

    @Autowired
    private HontPermissionDao hontPermissionDao;
    @Autowired
    private HontDepartmentHeaderDao hontDepartmentHeaderDao;
    @Override
    public HontUser findHontUserByUserName(String userName) {
        return hontUserDao.findByUserName(userName);
    }


    @Override
    public HontUser findByMobile(String mobile) {
        return hontUserDao.findByMobile(mobile);
    }

    @Override
    public List<HontUser> customFind(String mobile) {
        return hontUserDao.customFind(mobile);
    }

    @Override
    public HontUser cutomerfindByMobile(String mobile) {
        HontUser byMobile = hontUserDao.findByMobile(mobile);
        return byMobile;
    }

    @Override
    public HontUser findByUsername(String username) {
        HontUser user = hontUserDao.findByUserName(username);
        return user;
    }

    @Override
    public HontUser saveUser(HontUser user) {
        return hontUserDao.save(user);
    }

    @Override
    public HontUser updatePassword(HontUser user) {
        return hontUserDao.saveAndFlush(user);
    }

    @Override
    public HontUser updateNickname(HontUser user) {
        return hontUserDao.saveAndFlush(user);
    }

    @Override
    public HontUser updateMobile(HontUser mobile) {
        hontUserDao.updateMobileById(mobile.getMobile(), mobile.getId());
        return hontUserDao.findById(mobile.getId());
    }

    @Override
    public HontUser updateLastTime(HontUser user) {
        hontUserDao.updateLastTimeById(user.getUpdateLastTime(), user.getId());
        return hontUserDao.findById(user.getId());
    }

    @Override
    public HontUser updateOpenId(HontUser user) {
        hontUserDao.updateopenId(user.getOpenId(),user.getId());
        return hontUserDao.findById(user.getId());
    }

    @Override
    public HontUser findById(String id) {
        return hontUserDao.findById(id);
    }

    @Override
    public List<HontUser> hontUserList() {
        List<HontUser> all = hontUserDao.findAll();
        return all;
    }

    @Override
    public List<HontRole> hontRoleList() {
        List<HontRole> all = hontRoleDao.findAll();
        return all;
    }

    @Override
    public List<HontUserRole> hontUserRoleList() {
        List<HontUserRole> all = hontUserRoleDao.findAll();
        return all;
    }

    /*
     *查询当前用户，角色，权限
     * */
    @Override
    public UserRolePermissionVo getUserRolePermissionVo() {
        UserRolePermissionVo vo=new UserRolePermissionVo();
        HontUser currUser = securityUtil.getCurrUser();
        vo.setUser(currUser);
        List<HontUserRole> hontUserRolesByHontUserId = hontUserRoleDao.findHontUserRolesByHontUserId(currUser.getId());
        List<String> roleIds=new ArrayList<String>();
        List<HontRole> allByIds=new ArrayList<HontRole>();
       for(int i=0;i<hontUserRolesByHontUserId.size();i++){
           roleIds.add(hontUserRolesByHontUserId.get(i).getHontRoleId());
       }
       if(roleIds.size()>0){
         allByIds = hontRoleDao.findAllByIds(roleIds);

         vo.setRoles(allByIds);
       }
       /*
       *权限
       * */
       /* List<HontRolePermission> allByIds1 = hontRolePermissionDao.findAllByIds(roleIds);
        List<String> permissionIds=new ArrayList<String>();
        allByIds1.stream().forEach(hontRolePermissions ->{
            permissionIds.add(hontRolePermissions.getHontPermissionId());
        });
        List<HontPermission> allByIds2 = hontPermissionDao.findAllByIds(permissionIds);
        vo.setHontPermissions(allByIds2);*/
        /*
        * 管理部门信息
        * */
        List<HontDepartmentHeader> allByHontUserId = hontDepartmentHeaderDao.findAllByHontUserId(currUser.getId());
        List<String> departmentIds=new ArrayList<String>();
        allByHontUserId.stream().forEach(department->{
            departmentIds.add(department.getHontDepartmentId());
        });
        vo.setDepartmentIds(departmentIds);
        return vo;
    }

    @Override
    public List<String> getDepartmentIds() {
        HontUser currUser = securityUtil.getCurrUser();
        List<HontDepartmentHeader> allByHontUserId = hontDepartmentHeaderDao.findAllByHontUserId(currUser.getId());
        List<String> departmentIds=new ArrayList<String>();
        allByHontUserId.stream().forEach(department->{
            departmentIds.add(department.getHontDepartmentId());
        });
        return departmentIds;
    }

    @Override
    public HontUser findByOpenId(String openid) {
        return hontUserDao.findByOpenId(openid);
    }
   //logintype=1 用户名登录  logintype=1 手机登录登录
    @Override
    public boolean checkUserLoginType(String loginType, String username) {
        if(loginType.equals("1")){
            HontUser byUsername = this.findByUsername(username);
            if(byUsername!=null){
                return true;
            }
        }else {
            HontUser byUsername = this.findByMobile(username);
            if(byUsername!=null){
                return true;
            }
        }
        return false;
    }

    @Override
    public HontUser findHontUserByUserNameOrMobile(String userName) {

        return null;
    }
}
